﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Physic = StillDesign.PhysX; //PhysX
using PhysicMath = StillDesign.PhysX.MathPrimitives; //PhysX

namespace Game
{
    /*-------------------------------------------------------------------------
    | PhysicActorConstructor:
    | ------------
    | Physic actor constructor body
    |---------------------------------------------------------------------------*/
    public class PhysicBody : Body
    {
        public Physic.Actor Actor
        {
            get;
            set;
        }

        public PhysicBody(IDK.SceneNode node, Physic.Actor actor)
            : base(node)
        {
            Actor = actor;

            IsKinematic = actor.BodyFlags.Kinematic;
        }

        public override IDK.Vector3 GetPosition()
        {
            return IDKToPhysic.ToVector3(Actor.GlobalPosition);
        }

        public override void Reset()
        {
            throw new NotImplementedException();
        }

        public override void SetPosition(IDK.Vector3 pos)
        {
            Actor.BodyFlags.Kinematic = true;
            Actor.MoveGlobalPositionTo(IDKToPhysic.ToVector3(pos));
            Actor.BodyFlags.Kinematic = IsKinematic;
        }

        public override void Rotate(IDK.Quaternion q)
        {
            Actor.MoveGlobalOrientationTo(Actor.GlobalOrientationQuat * IDKToPhysic.ToQuaternion(q));
        }

        public override void AddLocalForce(IDK.Vector3 pos)
        {
            Actor.AddLocalForce(IDKToPhysic.ToVector3(pos), Physic.ForceMode.Force);
        }

        public override void AddLocalImpulse(IDK.Vector3 pos)
        {
            Actor.AddLocalForce(IDKToPhysic.ToVector3(pos), Physic.ForceMode.Impulse);
        }

        public override void AddWorldForce(IDK.Vector3 pos)
        {
            Actor.AddForce(IDKToPhysic.ToVector3(pos), Physic.ForceMode.Force);
        }

        public override void AddWorldImpulse(IDK.Vector3 pos)
        {
            Actor.AddForce(IDKToPhysic.ToVector3(pos), Physic.ForceMode.Impulse);
        }

        public override void OnUpdate(Events.ProcessSceneUpdate evnt)
        {
            if (!Actor.IsSleeping)
            {
                IDK.Quaternion q = IDKToPhysic.ToQuaternion(Actor.GlobalOrientationQuat);
                IDK.Vector3 p = IDKToPhysic.ToVector3(Actor.GlobalPosition);

                SceneNode.GetTransformation().SetTransform(q, p);
            }
        }
    }
}
